# coding=utf-8
from urllib import request
import re
import pinyin.pinyin as pinyin
import json
import time
from lxml import etree


def getweather():
    res = request.urlopen('http://pv.sohu.com/cityjson')
    city_info=res.read().decode('gbk')
    print(city_info)
    addr = str(city_info).split('=')[1].split(',')[2].split('"')[3]  # 取出地址信息
    py = pinyin.get(addr,format='strip')
    provice = py.split('sheng', 1)[0].replace(' ', '')  # 获取省份
    city = py.split('shi')[0].split('sheng')[1].strip().replace(' ', '')  # 获取城市
    url = 'http://qq.ip138.com/weather/%s/%s.htm' % (provice, city)

    # 分析url可知某省某市的天气url即为上面格式
    wea_info = request.urlopen(url).read().decode('gbk')

    #解析html，获取一周天气里当天天气
    tree = etree.HTML(wea_info)
    nodes = tree.xpath("/descendant::table[@class='t12']/tr")
    n_nodes = nodes[1:]

    weathers = []
    for n in range(len(n_nodes)):
        items = n_nodes[n].xpath("td")
        weathers_items = []
        for r in items:
            if r.text is None:
                tq = r.xpath("img")
                qt_str = ''
                for i_tq in tq:
                    if qt_str == '':
                        qt_str = i_tq.get("alt")
                    else:
                        qt_str = qt_str + "转" + i_tq.get("alt")
                weathers_items.append(qt_str)
            else:
                weathers_items.append(r.text)
        weathers.append(weathers_items)

    # 从一周天气信息中取出当天天气
    #print(time.localtime())
    n_time = time.localtime()
    n_year = n_time.tm_year
    n_mon = n_time.tm_mon
    n_day = n_time.tm_mday
    todayweather = {"date": "", "weather": "", "temperature": "", "wind": "", "addr": "","icon":""}
    for i in range(len(weathers[0])):
        if weathers[0][i].find(str(n_year) + "-" + str(n_mon) + "-" + str(n_day)) != -1:
            for j in range(len(weathers)):
                if j == 0:
                    todayweather["date"] = weathers[j][i]
                elif j == 1:
                    todayweather["weather"] = weathers[j][i]
                    if str(weathers[j][i]).find("转"):
                        n_weather=str(weathers[j][i]).split("转")[-1]
                    else:
                        n_weather =str(weathers[j][i])

                    if str(n_weather).find("雨") >= 0:
                        todayweather["icon"] = r"&#xe649;"
                    elif str(n_weather).find("雪") >= 0:
                        todayweather["icon"] = r"&#xe64b;"
                    elif str(n_weather).find("晴") >= 0:
                        todayweather["icon"] = r"&#xe64d;"
                    elif str(n_weather).find("阴") >= 0:
                        todayweather["icon"] = r"&#xe64c;"
                    elif str(n_weather).find("多云") >= 0:
                        todayweather["icon"] = r"&#xe64e;"
                    elif str(n_weather).find("雨夹雪") >= 0:
                        todayweather["icon"] = r"&#xe64a;"
                    else:
                        todayweather["icon"] = r"&#xe64d;"
                elif j == 2:
                    todayweather["temperature"] = weathers[j][i]
                elif j == 3:
                    todayweather["wind"] = weathers[j][i]
        else:
            continue
    todayweather["addr"]=addr

    #todayweather["test"]=i18nGlobal.get_i18nvalue('系统消息')
    # import tools.globalvar as globalvar
    #
    # todayweather["test"] =globalvar.get_sysparamsobject('pos_test_rrr')
    return todayweather


#调用测试
#print(getweather())
